//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖保佑             永无BUG 
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//                  不见满街漂亮妹，哪个归得程序员？  

#include "WulinFinalRound.h"

WulinFinalRound::WulinFinalRound(){
	//1新手组 2高手组 3宗师组
	type = 0;
	//三四名赛
	round4 = 0;
	//决赛
	round5 = 0;
}

WulinFinalRound::~WulinFinalRound()
{
	reset();
}

WulinFinalRound* WulinFinalRound::clone(){
	auto ret=new  WulinFinalRound();
	if (type)
ret->type=type->clone();
for(auto bean : round1){
		ret->round1.push_back(bean->clone());
}
for(auto bean : round2){
		ret->round2.push_back(bean->clone());
}
for(auto bean : round3){
		ret->round3.push_back(bean->clone());
}
	if (round4)
ret->round4=round4->clone();
	if (round5)
ret->round5=round5->clone();
return ret;
}

void WulinFinalRound::reset(){
	if (type)
	delete type;
	type=0;
for(auto bean : round1){
	if (bean)
	delete bean;
	bean=0;
}
round1.clear();
for(auto bean : round2){
	if (bean)
	delete bean;
	bean=0;
}
round2.clear();
for(auto bean : round3){
	if (bean)
	delete bean;
	bean=0;
}
round3.clear();
	if (round4)
	delete round4;
	round4=0;
	if (round5)
	delete round5;
	round5=0;
}
/**
 * 读取字节缓存
 */
bool WulinFinalRound::read_from(ByteBuf& buf)
{
	//1新手组 2高手组 3宗师组
	type = new WulinPair();
	readBean(buf, *type);
	//第一轮 1/8决赛 (共8对)
	short round1_length = readInt16(buf);
	for (int i = 0; i < round1_length; i++) {
		auto bean =new WulinPair();
		readBean(buf, *bean);
		round1.push_back(bean);
	}
	//第二轮 1/4决赛 (共4对)
	short round2_length = readInt16(buf);
	for (int i = 0; i < round2_length; i++) {
		auto bean =new WulinPair();
		readBean(buf, *bean);
		round2.push_back(bean);
	}
	//第三轮 半决赛 (共2对)
	short round3_length = readInt16(buf);
	for (int i = 0; i < round3_length; i++) {
		auto bean =new WulinPair();
		readBean(buf, *bean);
		round3.push_back(bean);
	}
	//三四名赛
	round4 = new WulinPair();
	readBean(buf, *round4);
	//决赛
	round5 = new WulinPair();
	readBean(buf, *round5);
	
	return true;
}

/**
 * 写入字节缓存
 */
bool WulinFinalRound::write_to(ByteBuf& buf)
{
	//1新手组 2高手组 3宗师组
	writeBean(buf, *type);
	//第一轮 1/8决赛 (共8对)
	writeShort(buf, round1.size());
	for (int i = 0; i < round1.size(); i++) {
		writeBean(buf, *round1[i]);
	}
	//第二轮 1/4决赛 (共4对)
	writeShort(buf, round2.size());
	for (int i = 0; i < round2.size(); i++) {
		writeBean(buf, *round2[i]);
	}
	//第三轮 半决赛 (共2对)
	writeShort(buf, round3.size());
	for (int i = 0; i < round3.size(); i++) {
		writeBean(buf, *round3[i]);
	}
	//三四名赛
	writeBean(buf, *round4);
	//决赛
	writeBean(buf, *round5);
	
	return true;
}
